// 
//  DotNetNuke? - http://www.dotnetnuke.com
//  Copyright (c) 2002-2006
//  by DotNetNuke Corporation
// 
//  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
//  documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
//  the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
//  to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// 
//  The above copyright notice and this permission notice shall be included in all copies or substantial portions 
//  of the Software.
// 
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
//  TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
//  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
//  CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
//  DEALINGS IN THE SOFTWARE.
// 
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using DotNetNuke.Entities.Users;
using System.Collections;
using System;
using DotNetNuke.Common.Utilities;

namespace DotNetNuke.Modules.IWebCSharp
{
    public partial class WebService
    {
        [WebMethod(Description = " Retrieves IWeb Version *DotNetNuke* |IWEB Core| #IWEB Misc# !Portal! "), SoapHeader("IWebCredentials")]
        public string GetVersion()
        {
            string strVersion = "";
            try
            {

                IWebAuthendication objIWebAuthendication = new IWebAuthendication(IWebCredentials);
                if (!(objIWebAuthendication.ValidAndAuthorized()))
                {
                    return "Not Authorized";
                }

                DotNetNuke.Entities.Modules.DesktopModuleController objDesktopModules = new DotNetNuke.Entities.Modules.DesktopModuleController();

                ArrayList arr = objDesktopModules.GetDesktopModules();
                foreach (DotNetNuke.Entities.Modules.DesktopModuleInfo objDesktopModuleInfo in arr)
                {
                    if (objDesktopModuleInfo.ModuleName == "IWebCSharp")
                    {
                        strVersion = objDesktopModuleInfo.Version;
                    }
                }
            }
            catch (Exception ex)
            {
                strVersion = ex.Message;
            }

            return strVersion;
        }


        [WebMethod(Description = " Encrypt Data *DotNetNuke* |IWEB Core| #IWEB Misc# !Portal! "), SoapHeader("IWebCredentials")]
        public string EncryptData(string UnEncryptedData, string Key)
        {
            string strResponse = "";
            try
            {
                IWebAuthendication objIWebAuthendication = new IWebAuthendication(IWebCredentials);
                if (!(objIWebAuthendication.ValidAndAuthorized()))
                {
                    return "Not Authorized";
                }

                IWebEncryption objIWebEncryption = new IWebEncryption();
                strResponse = objIWebEncryption.Encryption(UnEncryptedData, Key);
                strResponse = HttpUtility.UrlEncode(strResponse);
            }
            catch (Exception ex)
            {
                strResponse = ex.Message;
            }
            return strResponse;
        }

        [WebMethod(Description = " Un-Encrypt Data *DotNetNuke* |IWEB Core| #IWEB Misc# !Portal! "), SoapHeader("IWebCredentials")]
        public string UnEncryptData(string EncryptedData, string Key)
        {
            string strResponse = "";
            try
            {
                IWebAuthendication objIWebAuthendication = new IWebAuthendication(IWebCredentials);
                if (!(objIWebAuthendication.ValidAndAuthorized()))
                {
                    return "Not Authorized";
                }

                IWebEncryption objIWebEncryption = new IWebEncryption();
                strResponse = objIWebEncryption.DeCrypt(HttpUtility.UrlDecode(EncryptedData), Key).ToString();
                strResponse = HttpUtility.UrlEncode(strResponse);
            }
            catch (Exception ex)
            {
                strResponse = ex.Message;
            }
            return strResponse;
        }


        [WebMethod(Description = " GetUser *DotNetNuke* |IWEB Core| #IWEB Misc# !Portal! "), SoapHeader("IWebCredentials")]
        public UserInfo GetUser(string Username)
        {
            UserInfo objResponse = new UserInfo();
            try
            {
                IWebAuthendication objIWebAuthendication = new IWebAuthendication(IWebCredentials);
                if (!(objIWebAuthendication.ValidAndAuthorized()))
                {
                    objResponse.UserID = -1;
                    objResponse.Username = "Not Authorized";
                    return objResponse;
                }

                objResponse = IWebUser.GetUserInfo(IWebCredentials.PortalID, Username);

                //  Do not return the password for a administrator 
                if ((objResponse.IsSuperUser == false & objResponse.IsInRole("Administrators") == false))
                {
                    objResponse.Membership.Password = UserController.GetPassword(ref objResponse, "").ToString();
                }
                else
                {
                    objResponse.Membership.Password = "Administrator password is supressed";
                }


            }
            catch (Exception ex)
            {
                objResponse.UserID = -1;
                objResponse.Username = ex.Message;
            }
            return objResponse;
        }


        [WebMethod(Description = " CreateUser *DotNetNuke* |IWEB Core| #IWEB Misc# !Portal! "), SoapHeader("IWebCredentials")]
        public string CreateUser(string NewUsername, string FirstName, string LastName, string DisplayName, string email, string Newpassword)
        {

            string objResponse = "Error: Unknown Error";

            IWebAuthendication objIWebAuthendication = new IWebAuthendication(IWebCredentials);
            if (!(objIWebAuthendication.ValidAndAuthorized()))
            {
                return "Not Authorized";
            }

            UserInfo oUserInfo = new UserInfo();
            oUserInfo.PortalID = IWebCredentials.PortalID;
            oUserInfo.Membership.Username = NewUsername;
            oUserInfo.Username = NewUsername;
            oUserInfo.FirstName = FirstName;
            oUserInfo.LastName = LastName;
            oUserInfo.DisplayName = DisplayName;
            oUserInfo.Membership.Email = email;
            oUserInfo.Email = email;
            oUserInfo.Membership.Approved = true;
            oUserInfo.AffiliateID = Null.NullInteger;
            oUserInfo.Membership.Password = Newpassword;

            DotNetNuke.Security.Membership.UserCreateStatus objUserCreateStatus = IWebUser.CreateUser(oUserInfo);

            if (objUserCreateStatus == DotNetNuke.Security.Membership.UserCreateStatus.Success)
            {
                objResponse = "Success";
            }
            else
            {
                objResponse = String.Format("Error: {0}", objUserCreateStatus.ToString());
            }

            return objResponse;

        }

    }

}


